﻿using Application.Purchase_hService;
using Domain.Entity.ord;
using Domain.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Domain.Entity.bs;
using Microsoft.Extensions.Caching.Distributed;
using System.Text.Json.Serialization;
using System.Text.Json;
using Domain.Entity.search;
using Application.ProductService;
using WMSManage.Filter;

namespace WMSManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//需要鉴权
    public class Purchase_hController : ControllerBase
    {
        private readonly IPurchase_hService purchase_HService;
        private readonly IDistributedCache distributedCache;

        public Purchase_hController(IPurchase_hService purchase_HService, IDistributedCache distributedCache)
        {
            this.purchase_HService = purchase_HService;
            this.distributedCache = distributedCache;
        }


        /// <summary>
        /// 分页查询进货单
        /// </summary>
        /// <param name="code">单号</param>
        /// <param name="status">单据状态</param>
        /// <param name="sup">供应商id</param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="CurrentPage"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FenYe<Purchase_h>> GetPurchase_h(string? code, int? status, int? sup, string? starttime, string? endtime,int CurrentPage)
        {
            FenYe<Purchase_h> fenYe = new FenYe<Purchase_h>()
            {
                CurrentPage = CurrentPage,
            };
           return await purchase_HService.GetPurchase_h(fenYe, code, status, sup, starttime, endtime);
        }

        /// <summary>
        /// 高级查询进货单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="starttotal"></param>
        /// <param name="endtotal"></param>
        /// <param name="startdate"></param>
        /// <param name="enddate"></param>
        /// <param name="startnum"></param>
        /// <param name="endnum"></param>
        /// <param name="status"></param>
        /// <param name="sup"></param>
        /// <param name="suplink"></param>
        /// <param name="dep"></param>
        /// <param name="user"></param>
        /// <param name="createname"></param>
        /// <param name="startcreatetime"></param>
        /// <param name="endcreatetime"></param>
        /// <param name="approvername"></param>
        /// <param name="startapprovertime"></param>
        /// <param name="endapprovertime"></param>
        /// <param name="CurrentPage"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FenYe<Purchase_h>> GetAdvancedPurchase_h(string? code, string? starttotal, string? endtotal, string? startdate, string? enddate, string? startnum, string? endnum, int? status, int? sup, string? suplink, int? dep, int? user, string? createname, string? startcreatetime, string? endcreatetime, string? approvername, string? startapprovertime, string? endapprovertime, int CurrentPage)
        {
            FenYe<Purchase_h> fenYe = new FenYe<Purchase_h>()
            {
                CurrentPage = CurrentPage,
            };
            return await purchase_HService.GetAdvancedPurchase_h(fenYe, code, starttotal, endtotal, startdate, enddate, startnum, endnum, status, sup, suplink, dep, user, createname, startcreatetime, endcreatetime, approvername, startapprovertime,endapprovertime);
        }



        /// <summary>
        /// 删除进货单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> DeletePurchase_h(int id)
        {
            return await purchase_HService.DeletePurchase_h(id);
        }



        /// <summary>
        /// 新增进货单
        /// </summary>
        /// <param name="addPurchase_HDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddPurchase_h(AddPurchase_hDto addPurchase_HDto)
        {
            return await purchase_HService.AddPurchase_h(addPurchase_HDto);
        }



        /// <summary>
        /// 根据id查询进货单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Purchase_h> GetPurchase_HById(int id)
        {
            return await purchase_HService.GetPurchase_HById(id);
        }


        /// <summary>
        /// 修改进货单
        /// </summary>
        /// <param name="updatePurchase_HDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdatePurchase_h(UpdatePurchase_hDto updatePurchase_HDto)
        {
            return await purchase_HService.UpdatePurchase_h(updatePurchase_HDto);
        }


        /// <summary>
        /// 修改单据状态
        /// </summary>
        /// <param name="purid">进货单id</param>
        /// <param name="status">单据状态</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateStatus(int purid, int status)
        {
            return await purchase_HService.UpdateStatus(purid, status);
        }


        /// <summary>
        /// 审核进货单
        /// </summary>
        /// <param name="purid"></param>
        /// <param name="status"></param>
        /// <param name="approvername"></param>
        /// <param name="approvertime"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> ApproverPurchase_h(int purid, int status, string approvername, string approvertime)
        {
            return await purchase_HService.ApproverPurchase_h(purid,status,approvername,approvertime);
        }


        /// <summary>
        /// 查询所有进货单据(未删除未入库部分入库)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Purchase_h>> GetAllPurchase_h()
        {
            return await purchase_HService.GetAllPurchase_h();
        }


        /// <summary>
        /// 根据货品id查询进货记录
        /// </summary>
        /// <param name="procid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Purchase_h>> GetAllPurchase_hBypid(int procid)
        {
            return await purchase_HService.GetAllPurchase_hBypid(procid);
        }



        /// <summary>
        /// 进货统计
        /// </summary>
        /// <param name="timeType"></param>
        /// <param name="code"></param>
        /// <param name="sup"></param>
        /// <param name="user"></param>
        /// <param name="dep"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PurchaseTJResult> Purchase_hTJ(string? timeType, string? code, int? sup, int? user, int? dep, string? starttime, string? endtime, int? page, int? pagesize)
        {
            //缓存键
            string key = "PurchaseTJResult" +timeType+code+sup+user+dep+starttime+endtime+page+pagesize;
            //从缓存中获取数据
            var data = await distributedCache.GetStringAsync(key);
            if (data != null)
            {
                //从缓存中查
                return JsonSerializer.Deserialize<PurchaseTJResult>(data);
            }
            //从数据库查
            var result =  await purchase_HService.Purchase_hTJ(timeType, code, sup, user, dep, starttime, endtime, page, pagesize);
            //将查询到的结果存入缓存
            if (result != null)
            {
                var option = new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)//10分钟
                };
                await distributedCache.SetStringAsync(key, JsonSerializer.Serialize(result), option);
            }
            return result;
            
        }


        /// <summary>
        /// 进货统计刷新
        /// </summary>
        /// <param name="timeType"></param>
        /// <param name="code"></param>
        /// <param name="sup"></param>
        /// <param name="user"></param>
        /// <param name="dep"></param>
        /// <param name="starttime"></param>
        /// <param name="endtime"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Refresh(string? timeType, string? code, int? sup, int? user, int? dep, string? starttime, string? endtime, int? page, int? pagesize)
        {
            //缓存键
            string key = "PurchaseTJResult" + timeType + code + sup + user + dep + starttime + endtime + page + pagesize;

            // 清除缓存键
            distributedCache.Remove(key);

            // 返回成功消息
            return Ok(new { success = true, message = "ok" });
        }



        /// <summary>
        /// 更新已退货数量
        /// </summary>
        /// <param name="purid"></param>
        /// <param name="returnnum"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateRNum(int purid, int returnnum, int status,int newnum)
        {
            return await purchase_HService.UpdateRNum(purid, returnnum, status,newnum);
        }
    }
}
